/**
 * Created by George on 2015-06-29.
 */
'use strict';

let debugLog = require('./function.js').debugLog;
console.log('Load O2O: ' + module.id);
let F = require('./function');
let Dao = require('./dao');
let shopDispatcher = new Dao.shop();

function* api() {
}
exports.api = api;
/**
 {
     "id":12345,
     "image": "xxx",
     "name": "testB",
     "tel": ["12345678"],
     "address": "广州天河大学城",
     "status": "1",
     "desc": "测试商店",
     "loc": [113.267525, 23.123222],
     "district": [
         {"name": "广东省","pCode": 0,"code": 3},
         {"name": "广州市","pCode": 1,"code": 12},
         {"name": "天河","pCode": 2,"code": 13}
     ],
     "other_info": "哈哈",
     "categories": [{"name": "美发","code": 1,"pCode": 1}],
     "act": 1
 }
 * @param Env
 */
/**
 * @todo 添加商店
 * @param Env
 */
api.add = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2, 3]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield shopDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};
/**
 * todo 更新店铺
 * @param Env
 */
api.update = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield shopDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 删除一个商铺
 *  post /o2o_service/shop/v1/delete/
 * {
 *  "id":123,
 * }
 * @param Env
 */
api.delete = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield shopDispatcher.delete(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret);
};

/**
 * 根据条件查找商店
 * post /o2o_service/shop/v1/list
 {
     "cateCodes": 1,
     "districtCode":440000,
     "pageSize": 2,
     "page": 2,
     "sort":"grade",
     "act":1
 }

 * 查询 [广州市内,xx商店,按评分排序]
 *
 **************************************
 *
 {
     "cateCodes": 1,
     "radius": 130,
     "loc":[113.267525,23.123222],
     "sort": "grade",
     "pageSize": 5,
     "page": 1,
     "act":1
 }
 * 查询 [距离(11,22)在2000m范围内的理发店，按照距离|评分排序]
 * @param Env
 */
api.list = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2, 3, 4, 5, 6, 7, 8]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  let ret
  if (fields.act == 7) ret = yield shopDispatcher.listByQut(fields);
  else ret = yield shopDispatcher.list(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 获取某个商店详情
 * /o2o_service/shop/v1/detail/
 * @param env
 */
api.detail = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;

  let ret = yield shopDispatcher.detail(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * todo 获取商店的district
 * @param Env
 */
api.getDistrict = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;

  let ret = yield shopDispatcher.getDistrict(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
}
